package com.Lixiaoqian.Gulangyu.route_m

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.Lixiaoqian.Gulangyu.R
import com.Lixiaoqian.Gulangyu.base.BaseActivity
import com.Lixiaoqian.Gulangyu.base.QianTechApp
import com.Lixiaoqian.Gulangyu.utils.Constant
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MyLocationConfiguration
import com.baidu.mapapi.map.MyLocationData
import com.yinglan.scrolllayout.ScrollLayout
import kotlinx.android.synthetic.main.activity_route_detail.*
import kotlinx.android.synthetic.main.layout_base.*
import lixiaoqian.com.skinproject.utils.DensityUtil


class RouteDetailActivity : BaseActivity() {
    lateinit var baiduMap: BaiduMap
    override fun barRightEvent() {

    }

    companion object {
        fun startActivity(fromActivity: Activity) {
            fromActivity.startActivity(Intent(fromActivity, RouteDetailActivity::class.java))
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_route_detail)
        initMap()
        initView()
    }

    private fun initView() {
        wd_toolbar.isShowLeftAndRightImage(View.VISIBLE,View.GONE)
        /**设置 ScrollLayout*/
        scroll_down_layout.apply {
            setMinOffset(0)
            setMaxOffset(DensityUtil.dip2px(this@RouteDetailActivity, 200f))
            setExitOffset(DensityUtil.dip2px(this@RouteDetailActivity, 200f))
            setIsSupportExit(true)
            setToExit()
            setOnScrollChangedListener(object : ScrollLayout.OnScrollChangedListener {
                override fun onScrollFinished(currentStatus: ScrollLayout.Status?) {
                    if (currentStatus == ScrollLayout.Status.OPENED) {
                        wd_toolbar.setTitle("鼓浪屿路线")
                    } else {
                        wd_toolbar.setTitle("任务点")
                    }
                }

                override fun onChildScroll(top: Int) {
                }

                override fun onScrollProgressChanged(currentProgress: Float) {
                }

            })
        }
    }

    private fun initMap() {
        baiduMap = wd_mapView.map
        val configuration = MyLocationConfiguration(MyLocationConfiguration.LocationMode.FOLLOWING, true, null)
        val qianTechApp = this.application as QianTechApp
        // 构造定位数据
        val locData = MyLocationData.Builder()
                .accuracy(qianTechApp.currentPosition.radius!!)
                // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(qianTechApp.currentPosition.diretion!!).latitude(qianTechApp.currentPosition.latitude!!)
                .longitude(qianTechApp.currentPosition.longitude!!).build()
        baiduMap.apply {
            mapType = BaiduMap.MAP_TYPE_NORMAL
            setMyLocationConfiguration(configuration)
            baiduMap.isMyLocationEnabled = true
            setMyLocationData(locData)
            setMapStatus(MapStatusUpdateFactory.zoomTo(Constant.MAP_ZOOM))
        }
    }

    override fun onResume() {
        super.onResume()
        wd_mapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        wd_mapView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        baiduMap.isMyLocationEnabled = false
        wd_mapView.onDestroy()
    }
}
